package com.sgcc.operate.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.sgcc.common.core.utils.ServletUtils;
import com.sgcc.common.datascope.annotation.DataScope;
import com.sgcc.common.security.service.TokenService;
import com.sgcc.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import com.sgcc.operate.domain.OIndiDefi;
import com.sgcc.operate.service.IOIndiDefiService;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 运营指标定义Controller
 *
 * @author sgcc
 * @date 2021-03-23
 */
@RestController
@RequestMapping("/indidefi")
public class OIndiDefiController extends BaseController {
    @Autowired
    private IOIndiDefiService oIndiDefiService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询运营指标定义列表
     */
    //@PreAuthorize(hasPermi = "operate:defi:list")
    @GetMapping("/list")
    @DataScope(deptAlias = "o2")
    public TableDataInfo list(OIndiDefi oIndiDefi) {
        startPage();
        List<OIndiDefi> list = oIndiDefiService.selectOIndiDefiList(oIndiDefi);
        return getDataTable(list);
    }

    /**
     * 导出运营指标定义列表
     */
    //@PreAuthorize(hasPermi = "operate:defi:export")
    @Log(title = "运营指标定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @DataScope(deptAlias = "o2")
    public void export(HttpServletResponse response, OIndiDefi oIndiDefi) throws IOException {
        List<OIndiDefi> list = oIndiDefiService.selectOIndiDefiList(oIndiDefi);
        ExcelUtil<OIndiDefi> util = new ExcelUtil<OIndiDefi>(OIndiDefi.class);
        util.exportExcel(response, list, "defi");
    }

    /**
     * 获取运营指标定义详细信息
     */
    //@PreAuthorize(hasPermi = "operate:defi:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(oIndiDefiService.selectOIndiDefiById(id));
    }

    /**
     * 新增运营指标定义
     */
    //@PreAuthorize(hasPermi = "operate:defi:add")
    @Log(title = "运营指标定义", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OIndiDefi oIndiDefi) {
        OIndiDefi se = new OIndiDefi();
        se.setIndiname(oIndiDefi.getIndiname());
        List<OIndiDefi> oIndiDefis = oIndiDefiService.selectOIndiDefiList(se);
        if (oIndiDefis.size() > 0) {
            return AjaxResult.error("指标名称已存在");
        }

        return toAjax(oIndiDefiService.insertOIndiDefi(oIndiDefi));
    }

    /**
     * 修改运营指标定义
     */
    //@PreAuthorize(hasPermi = "operate:defi:edit")
    @Log(title = "运营指标定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OIndiDefi oIndiDefi) {
        OIndiDefi se = new OIndiDefi();
        se.setIndiname(oIndiDefi.getIndiname());
        List<OIndiDefi> oIndiDefis = oIndiDefiService.selectOIndiDefiList(se);
        if (oIndiDefis.size() > 0 && !oIndiDefis.get(0).getId().equals(oIndiDefi.getId())) {
            return AjaxResult.error("指标名称已存在");
        }
        return toAjax(oIndiDefiService.updateOIndiDefi(oIndiDefi));
    }

    /**
     * 删除运营指标定义
     */
    //@PreAuthorize(hasPermi = "operate:defi:remove")
    @Log(title = "运营指标定义", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        String i = oIndiDefiService.deleteOIndiDefiByIds(ids);
        if (i.length() > 4){
            return AjaxResult.error(201,i);
        }else {
            return AjaxResult.success(i);
        }
    }


    /**
     * 查询运营指标定义列表
     */
    //@PreAuthorize(hasPermi = "operate:defi:list")
    @GetMapping("/optionselectlist")
    public TableDataInfo optionselectlist(OIndiDefi oIndiDefi) {
        startPage();
        oIndiDefi.setModifyorg(tokenService.getLoginUser().getSysUser().getDeptId());
        List<OIndiDefi> list = oIndiDefiService.selectOIndiDefiList(oIndiDefi);
        return getDataTable(list);
    }


    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<OIndiDefi> util = new ExcelUtil<OIndiDefi>(OIndiDefi.class);
        List<OIndiDefi> userList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = oIndiDefiService.importIndiDefi(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

}
